Micron Document
<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Two's complement</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Two's_complement"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.math.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Two_s_complement rootpage-Two_s_complement skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Two's complement</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p><b>Two's complement</b> is the most common <a href="Signed_number_representations" title="Signed number representations">method of representing signed</a> (positive, negative, and zero) <a href="Integer_(computer_science)" title="Integer (computer science)">integers</a> on computers,<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> and more generally, <a href="Fixed-point_arithmetic" title="Fixed-point arithmetic">fixed point binary</a> values. As with the <a href="Ones'_complement" title="Ones' complement">ones' complement</a> and <a href="Sign-magnitude" class="mw-redirect" title="Sign-magnitude">sign-magnitude</a> systems, two's complement uses the <a href="Most_significant_bit" class="mw-redirect" title="Most significant bit">most significant bit</a> as the <i>sign</i> to indicate positive (0) or negative (1) numbers, and nonnegative numbers are given their unsigned representation (6 is 0110, zero is 0000); however, in two's complement, negative numbers are represented as the <a href="Bit_complement" class="mw-redirect" title="Bit complement">bit complement</a> of their magnitude plus 1 (−6 is 1010). The number of bits in the representation may be increased by padding all additional high bits of positive or negative numbers with 1's or 0's, respectively, or decreased by removing additional leading 1's or 0's.
</p><p>Unlike the <a href="Ones'_complement" title="Ones' complement">ones' complement</a> scheme, the two's complement scheme has only one representation for zero, with room for one extra negative number (the range of a 4-bit number is -8 to +7). Furthermore, the same arithmetic implementations can be used on signed as well as unsigned integers<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
and differ only in the integer overflow situations, since the sum of representations of a positive number and its negative is 0 (with the carry bit set).
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Procedure">Procedure</h2></div>
<p>The following is the procedure for obtaining the two's complement representation of a given <i>negative</i> number in binary digits:
</p>
<ul><li>Step 1: starting with the absolute binary representation of the number, with the leading bit being a sign bit;<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup></li>
<li>Step 2: inverting (or flipping) all bits – changing every 0 to 1, and every 1 to 0;</li>
<li>Step 3: adding 1 to the entire inverted number, ignoring any <a href="Integer_overflow" title="Integer overflow">overflow</a>. Accounting for overflow will produce the wrong value for the result.</li></ul>
<p>For example, to calculate the <a href="Decimal" title="Decimal">decimal</a> number <b>−6</b> in binary from the number <b>6</b>:
</p>
<ul><li>Step 1: <i>+6</i> in decimal is <i>0110</i> in binary; the leftmost significant bit (the first 0) is the <a href="Sign_(mathematics)" title="Sign (mathematics)">sign</a> (just 110 in binary would be −2 in decimal).</li>
<li>Step 2: flip all bits in <i>0110</i>, giving <i>1001</i>.</li>
<li>Step 3: add the place value 1 to the flipped number <i>1001</i>, giving <i>1010</i>.</li></ul>
<p>To verify that <i>1010</i> indeed has a value of <i>−6</i>, add the place values together, but <i>subtract</i> the sign value from the final calculation. Because the most significant value is the sign value, it must be subtracted to produce the correct result: <b>1010</b> = <b>−</b>(<b>1</b>×2<sup>3</sup>) + (<b>0</b>×2<sup>2</sup>) + (<b>1</b>×2<sup>1</sup>) + (<b>0</b>×2<sup>0</sup>) = <b>1</b>×−8 + <b>0</b> + <b>1</b>×2 + <b>0</b> = −6.
</p>
<table class="wikitable">
<tbody><tr>
<td>Bits:
</td>
<td>1
</td>
<td>0
</td>
<td>1
</td>
<td>0
</td></tr>
<tr>
<td>Decimal bit value:
</td>
<td><b>−</b>8
</td>
<td>4
</td>
<td>2
</td>
<td>1
</td></tr>
<tr>
<td>Binary calculation:
</td>
<td><b>−</b>(<b>1</b>×2<sup>3</sup>)
</td>
<td>(<b>0</b>×2<sup>2</sup>)
</td>
<td>(<b>1</b>×2<sup>1</sup>)
</td>
<td>(<b>0</b>×2<sup>0</sup>)
</td></tr>
<tr>
<td>Decimal calculation:
</td>
<td><b>−</b>(<b>1</b>×8)
</td>
<td><b>0</b>
</td>
<td><b>1</b>×2
</td>
<td><b>0</b>
</td></tr></tbody></table>
<p>Note that steps 2 and 3 together are a valid method to compute the <a href="Additive_inverse" title="Additive inverse">additive inverse</a> <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle -n}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mo>−<!-- − --></mo>
<mi>n</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle -n}</annotation>
</semantics>
</math></span><img src="./4f00139753ecf4fe00a10a17bd5620b70a61b29e.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.505ex; width:3.203ex; height:2.176ex;" alt="{\displaystyle -n}" loading="lazy"></span> of any (positive or negative) integer <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle n}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>n</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle n}</annotation>
</semantics>
</math></span><img src="./a601995d55609f2d9f5e233e36fbe9ea26011b3b.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.395ex; height:1.676ex;" alt="{\displaystyle n}" loading="lazy"></span> where both input and output are in two's complement format. An alternative to compute <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle -n}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mo>−<!-- − --></mo>
<mi>n</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle -n}</annotation>
</semantics>
</math></span><img src="./4f00139753ecf4fe00a10a17bd5620b70a61b29e.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.505ex; width:3.203ex; height:2.176ex;" alt="{\displaystyle -n}" loading="lazy"></span> is to use subtraction <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 0-n}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mn>0</mn>
<mo>−<!-- − --></mo>
<mi>n</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle 0-n}</annotation>
</semantics>
</math></span><img src="./f66a4ee6484f7c8d709cd7d374fd35b1e9d242e1.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.505ex; width:5.398ex; height:2.343ex;" alt="{\displaystyle 0-n}" loading="lazy"></span>. See below for subtraction of integers in two's complement format.
</p>
<div class="mw-heading mw-heading2"><h2 id="Theory">Theory</h2></div>
<p>Two's complement is an example of a <a href="Method_of_complements" title="Method of complements">radix complement</a>. The 'two' in the name refers to the number <span class="texhtml">2<sup><i>N</i></sup></span> - "two to the power of N", which is the value in respect to which the complement is calculated in an <span class="texhtml mvar" style="font-style:italic;">N</span>-bit system (the only case where exactly 'two' would be produced in this term is <span class="texhtml"><i>N</i> = 1</span>, so for a 1-bit system, but these do not have capacity for both a sign and a zero). As such, the precise definition of the <i>two's complement</i> of an <span class="texhtml mvar" style="font-style:italic;">N</span>-bit number is the <a href="Method_of_complements" title="Method of complements">complement</a> of that number with respect to <span class="texhtml">2<sup><i>N</i></sup></span>.
</p><p>The defining property of being a <i>complement to a number with respect to <span class="texhtml">2<sup>N</sup></span></i> is simply that the summation of this number with the original produce <span class="texhtml">2<sup><i>N</i></sup></span>. For example, using binary with numbers up to three bits (so <span class="texhtml"><i>N</i> = 3</span> and <span class="texhtml">2<sup><i>N</i></sup> = 2<sup>3</sup> = 8 = 1000<sub>2</sub></span>, where '<sub>2</sub>' indicates a binary representation), a two's complement for the number 3 (<span class="texhtml">011<sub>2</sub></span>) is 5 (<span class="texhtml">101<sub>2</sub></span>), because summed to the original it gives <span class="texhtml">2<sup>3</sup> = 1000<sub>2</sub> = 011<sub>2</sub> + 101<sub>2</sub></span>. Where this correspondence is employed for representing negative numbers, it effectively means, using an analogy with decimal digits and a number-space only allowing eight non-negative numbers 0 through 7, dividing the number-space into two sets: the first four of the numbers 0 1 2 3 remain the same, while the remaining four encode negative numbers, maintaining their growing order, so making 4 encode −4, 5 encode −3, 6 encode −2 and 7 encode −1. A binary representation has an additional utility however, because the most significant bit also indicates the group (and the sign): it is 0 for the first group of non-negatives, and 1 for the second group of negatives. The tables at right illustrate this property.
</p>
<table class="wikitable sortable floatright" style="text-align: center;">
<caption>Three-bit integers
</caption>
<tbody><tr>
<th>Bits
</th>
<th>Unsigned value
</th>
<th>Signed value<br>(Two's complement)
</th></tr>
<tr>
<td>000
</td>
<td>0
</td>
<td>0
</td></tr>
<tr>
<td>001
</td>
<td>1
</td>
<td>1
</td></tr>
<tr>
<td>010
</td>
<td>2
</td>
<td>2
</td></tr>
<tr>
<td>011
</td>
<td>3
</td>
<td>3
</td></tr>
<tr>
<td>100
</td>
<td>4
</td>
<td>−4
</td></tr>
<tr>
<td>101
</td>
<td>5
</td>
<td>−3
</td></tr>
<tr>
<td>110
</td>
<td>6
</td>
<td>−2
</td></tr>
<tr>
<td>111
</td>
<td>7
</td>
<td>−1
</td></tr></tbody></table>
<table class="wikitable sortable floatright" style="text-align: center;">
<caption>Eight-bit integers
</caption>
<tbody><tr>
<th>Bits
</th>
<th>Unsigned value
</th>
<th>Signed value <br>(Two's complement)
</th></tr>
<tr>
<td>0000&nbsp;0000
</td>
<td>0
</td>
<td>0
</td></tr>
<tr>
<td>0000 0001
</td>
<td>1
</td>
<td>1
</td></tr>
<tr>
<td>0000 0010
</td>
<td>2
</td>
<td>2
</td></tr>
<tr>
<td>0111 1110
</td>
<td>126
</td>
<td>126
</td></tr>
<tr>
<td>0111 1111
</td>
<td>127
</td>
<td>127
</td></tr>
<tr>
<td>1000 0000
</td>
<td>128
</td>
<td>−128
</td></tr>
<tr>
<td>1000 0001
</td>
<td>129
</td>
<td>−127
</td></tr>
<tr>
<td>1000 0010
</td>
<td>130
</td>
<td>−126
</td></tr>
<tr>
<td>1111 1110
</td>
<td>254
</td>
<td>−2
</td></tr>
<tr>
<td>1111 1111
</td>
<td>255
</td>
<td>−1
</td></tr></tbody></table>
<p>Calculation of the binary two's complement of a positive number essentially means subtracting the number from the <span class="texhtml">2<sup><i>N</i></sup></span>. But as can be seen for the three-bit example and the four-bit <span class="texhtml">1000<sub>2</sub></span> (<span class="texhtml">2<sup>3</sup></span>), the number <span class="texhtml">2<sup><i>N</i></sup></span> will not itself be representable in a system limited to <span class="texhtml mvar" style="font-style:italic;"><i>N</i></span> bits, as it is just outside the <span class="texhtml mvar" style="font-style:italic;"><i>N</i></span> bits space (the number is nevertheless the reference point of the "Two's complement" in an <span class="texhtml mvar" style="font-style:italic;"><i>N</i></span>-bit system). Because of this, systems with maximally <span class="texhtml mvar" style="font-style:italic;"><i>N</i></span>-bits must break the subtraction into two operations: first subtract from the maximum number in the <span class="texhtml mvar" style="font-style:italic;"><i>N</i></span>-bit system, that is <span class="texhtml">2<sup><i>N</i></sup>−1</span> (this term in binary is actually a simple number consisting of 'all 1s', and a subtraction from it can be done simply by inverting all bits in the number also known as <a href="Bitwise_operation#NOT" title="Bitwise operation">the bitwise NOT operation</a>) and then adding the one. Coincidentally, that intermediate number before adding the one is also used in computer science as another method of signed number representation and is called a <a href="Ones'_complement" title="Ones' complement">ones' complement</a> (named that because summing such a number with the original gives the 'all 1s').
</p><p>Compared to other systems for representing signed numbers (e.g., <a href="Ones'_complement" title="Ones' complement">ones' complement</a>), the two's complement has the advantage that the fundamental arithmetic operations of <a href="Addition" title="Addition">addition</a>, <a href="Subtraction" title="Subtraction">subtraction</a>, and <a href="Multiplication" title="Multiplication">multiplication</a> are identical to those for unsigned binary numbers (as long as the inputs are represented in the same number of bits as the output, and any <a href="Integer_overflow" title="Integer overflow">overflow</a> beyond those bits is discarded from the result). This property makes the system simpler to implement, especially for higher-precision arithmetic. Additionally, unlike ones' complement systems, two's complement has no representation for <a href="Signed_zero" title="Signed zero">negative zero</a>, and thus does not suffer from its associated difficulties. Otherwise, both schemes have the desired property that the sign of integers can be reversed by taking the complement of its binary representation, but two's complement has an exception – the lowest negative, as can be seen in the tables.<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="History">History</h2></div>
<p>The <a href="Method_of_complements" title="Method of complements">method of complements</a> had long been used to perform subtraction in decimal <a href="Adding_machine" title="Adding machine">adding machines</a> and <a href="Mechanical_calculator" title="Mechanical calculator">mechanical calculators</a>. <a href="John_von_Neumann" title="John von Neumann">John von Neumann</a> suggested use of two's complement binary representation in his 1945 <i><a href="First_Draft_of_a_Report_on_the_EDVAC" title="First Draft of a Report on the EDVAC">First Draft of a Report on the EDVAC</a></i> proposal for an electronic stored-program digital computer.<sup id="cite_ref-von_Neumann_1945_5-0" class="reference"><a href="#cite_note-von_Neumann_1945-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup> The 1949 <a href="EDSAC" title="EDSAC">EDSAC</a>, which was inspired by the <i>First Draft</i>, used two's complement representation of negative binary integers.
</p><p>Many early computers, including the <a href="CDC_6600" title="CDC 6600">CDC 6600</a>, the <a href="LINC" title="LINC">LINC</a>, the <a href="PDP-1" title="PDP-1">PDP-1</a>, and the UNIVAC 1107, use <a href="Ones'_complement" title="Ones' complement">ones' complement</a> notation; the descendants of the UNIVAC 1107, the <a href="UNIVAC_1100/2200_series" title="UNIVAC 1100/2200 series">UNIVAC 1100/2200 series</a>, continued to do so. The <a href="IBM_700/7000_series" title="IBM 700/7000 series">IBM 700/7000 series</a> scientific machines use sign/magnitude notation, except for the index registers which are two's complement. Early commercial computers storing negative values in two's complement form include the <a href="English_Electric_DEUCE" title="English Electric DEUCE">English Electric DEUCE</a> (1955) and the <a href="Digital_Equipment_Corporation" title="Digital Equipment Corporation">Digital Equipment Corporation</a> <a href="PDP-5" title="PDP-5">PDP-5</a> (1963) and <a href="PDP-6" title="PDP-6">PDP-6</a> (1964). The <a href="IBM_System/360" title="IBM System/360">System/360</a>, introduced in 1964 by <a href="IBM" title="IBM">IBM</a>, then the dominant player in the computer industry, made two's complement the most widely used binary representation in the computer industry. The first minicomputer, the <a href="PDP-8" title="PDP-8">PDP-8</a> introduced in 1965, uses two's complement arithmetic, as do the 1969 <a href="Data_General_Nova" title="Data General Nova">Data General Nova</a>, the 1970 <a href="PDP-11" title="PDP-11">PDP-11</a>, and almost all subsequent minicomputers and microcomputers.
</p>
<div class="mw-heading mw-heading2"><h2 id="Converting_from_two's_complement_representation">Converting from two's complement representation</h2></div>
<p>A two's-complement number system encodes positive and negative numbers in a binary number representation. The weight of each bit is a power of two, except for the <a href="Most_significant_bit" class="mw-redirect" title="Most significant bit">most significant bit</a>, whose weight is the negative of the corresponding power of two.
</p><p>The value&nbsp;<span class="texhtml mvar" style="font-style:italic;">w</span> of an <span class="texhtml mvar" style="font-style:italic;">N</span>-bit integer <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle a_{N-1}a_{N-2}\dots a_{0}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msub>
<mi>a</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
<mo>−<!-- − --></mo>
<mn>1</mn>
</mrow>
</msub>
<msub>
<mi>a</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
<mo>−<!-- − --></mo>
<mn>2</mn>
</mrow>
</msub>
<mo>…<!-- … --></mo>
<msub>
<mi>a</mi>
<mrow class="MJX-TeXAtom-ORD">
<mn>0</mn>
</mrow>
</msub>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle a_{N-1}a_{N-2}\dots a_{0}}</annotation>
</semantics>
</math></span><img src="./6733b85a0ac623ae6aaa53d4a52b97b9a737611d.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.671ex; width:15.825ex; height:2.009ex;" alt="{\displaystyle a_{N-1}a_{N-2}\dots a_{0}}" loading="lazy"></span> is given by the following formula:
</p><p><span class="mwe-math-element mwe-math-element-block"><span class="mwe-math-mathml-display mwe-math-mathml-a11y" style="display: none;"><math display="block" xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle w=-a_{N-1}2^{N-1}+\sum _{i=0}^{N-2}a_{i}2^{i}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>w</mi>
<mo>=</mo>
<mo>−<!-- − --></mo>
<msub>
<mi>a</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
<mo>−<!-- − --></mo>
<mn>1</mn>
</mrow>
</msub>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
<mo>−<!-- − --></mo>
<mn>1</mn>
</mrow>
</msup>
<mo>+</mo>
<munderover>
<mo>∑<!-- ∑ --></mo>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
<mo>=</mo>
<mn>0</mn>
</mrow>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
<mo>−<!-- − --></mo>
<mn>2</mn>
</mrow>
</munderover>
<msub>
<mi>a</mi>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msub>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>i</mi>
</mrow>
</msup>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle w=-a_{N-1}2^{N-1}+\sum _{i=0}^{N-2}a_{i}2^{i}}</annotation>
</semantics>
</math></span></span>
</p><p>The most significant bit determines the sign of the number and is sometimes called the <a href="Sign_bit" title="Sign bit">sign bit</a>. Unlike in <a href="Sign-and-magnitude" class="mw-redirect" title="Sign-and-magnitude">sign-and-magnitude</a> representation, the sign bit also has the weight <span class="texhtml">−(2<sup><i>N</i> − 1</sup>)</span> shown above. Using <span class="texhtml mvar" style="font-style:italic;">N</span> bits, all integers from <span class="texhtml">−(2<sup><i>N</i> − 1</sup>)</span> to <span class="texhtml">2<sup><i>N</i> − 1</sup> − 1</span> can be represented.
</p>
<div class="mw-heading mw-heading2"><h2 id="Converting_to_two's_complement_representation">Converting to two's complement representation</h2></div>
<p>In two's complement notation, a <i>non-negative</i> number is represented by its ordinary <a href="Binary_numeral_system" class="mw-redirect" title="Binary numeral system">binary representation</a>; in this case, the most significant bit is 0. Though, the range of numbers represented is not the same as with unsigned binary numbers. For example, an 8-bit unsigned number can represent the values 0 to 255 (11111111). However a two's complement 8-bit number can only represent non-negative integers from 0 to 127 (01111111), because the rest of the bit combinations with the most significant bit as '1' represent the negative integers −1 to −128.
</p><p>The two's complement operation is the <a href="Additive_inverse" title="Additive inverse">additive inverse</a> operation, so negative numbers are represented by the two's complement of the <a href="Absolute_value" title="Absolute value">absolute value</a>.
</p>
<div class="mw-heading mw-heading3"><h3 id="From_the_ones'_complement">From the ones' complement</h3></div>
<p>To get the two's complement of a negative binary number, all <a href="Bit" title="Bit">bits</a> are inverted, or "flipped", by using the <a href="Bitwise_NOT" class="mw-redirect" title="Bitwise NOT">bitwise NOT</a> operation; the value of 1 is then added to the resulting value, ignoring the overflow which occurs when taking the two's complement of 0.
</p><p>For example, using 1 byte (=8 bits), the decimal number 5 is represented by
</p>
<style data-mw-deduplicate="TemplateStyles:r996643573">
/* start https://en.wikipedia.org/ */


.mw-parser-output .block-indent{padding-left:3em;padding-right:0;overflow:hidden}


/* end https://en.wikipedia.org/ */
</style><div class="block-indent">0000 0101<sub>2</sub></div>
<p>The most significant bit (the leftmost bit in this case) is 0, so the pattern represents a non-negative value. To convert to −5 in two's-complement notation, first, all bits are inverted, that is: 0 becomes 1 and 1 becomes 0:
</p>
<div class="block-indent">1111 1010<sub>2</sub></div>
<p>At this point, the representation is the <a href="Ones'_complement" title="Ones' complement">ones' complement</a> of the decimal value −5. To obtain the two's complement, 1 is added to the result, giving:
</p>
<div class="block-indent">1111 1011<sub>2</sub></div>
<p>The result is a signed binary number representing the decimal value −5 in two's-complement form. The most significant bit is 1, signifying that the value represented is negative.
</p><p>Alternatively, instead of adding 1 after inverting a positive binary number, 1 can be subtracted from the number <i>before</i> it is inverted. The two methods can easily be shown to be equivalent. The inversion (ones' complement) of <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle x}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>x</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle x}</annotation>
</semantics>
</math></span><img src="./87f9e315fd7e2ba406057a97300593c4802b53e4.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.33ex; height:1.676ex;" alt="{\displaystyle x}" loading="lazy"></span> equals <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle (2^{N}-1)-x}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mo stretchy="false">(</mo>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
</mrow>
</msup>
<mo>−<!-- − --></mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
<mo>−<!-- − --></mo>
<mi>x</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle (2^{N}-1)-x}</annotation>
</semantics>
</math></span><img src="./29447de3f9d361f2157f808f49d35bde94247fa4.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:12.836ex; height:3.176ex;" alt="{\displaystyle (2^{N}-1)-x}" loading="lazy"></span>, so the sum of the inversion and 1 equals <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle (2^{N}-1)-x+1=}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mo stretchy="false">(</mo>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
</mrow>
</msup>
<mo>−<!-- − --></mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
<mo>−<!-- − --></mo>
<mi>x</mi>
<mo>+</mo>
<mn>1</mn>
<mo>=</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle (2^{N}-1)-x+1=}</annotation>
</semantics>
</math></span><img src="./5a9ccb12be7b325919078b9b63ac0a99d9df4afc.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:19.292ex; height:3.176ex;" alt="{\displaystyle (2^{N}-1)-x+1=}" loading="lazy"></span><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 2^{N}-x-1+1=}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
</mrow>
</msup>
<mo>−<!-- − --></mo>
<mi>x</mi>
<mo>−<!-- − --></mo>
<mn>1</mn>
<mo>+</mo>
<mn>1</mn>
<mo>=</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle 2^{N}-x-1+1=}</annotation>
</semantics>
</math></span><img src="./4783d3ddcca0347ccbb92d43e92a731e19445e9b.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.505ex; width:17.483ex; height:2.843ex;" alt="{\displaystyle 2^{N}-x-1+1=}" loading="lazy"></span><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 2^{N}-x}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
</mrow>
</msup>
<mo>−<!-- − --></mo>
<mi>x</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle 2^{N}-x}</annotation>
</semantics>
</math></span><img src="./5dbf56f2535fd073f083e7ea417fc5f448dcd415.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.505ex; width:7.024ex; height:2.843ex;" alt="{\displaystyle 2^{N}-x}" loading="lazy"></span>, which equals the two's complement of <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle x}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>x</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle x}</annotation>
</semantics>
</math></span><img src="./87f9e315fd7e2ba406057a97300593c4802b53e4.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.33ex; height:1.676ex;" alt="{\displaystyle x}" loading="lazy"></span> as expected. The inversion of <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle x-1}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>x</mi>
<mo>−<!-- − --></mo>
<mn>1</mn>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle x-1}</annotation>
</semantics>
</math></span><img src="./0f1a88d34243b98b57c4df9db5724f61b59a4b9d.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.505ex; width:5.333ex; height:2.343ex;" alt="{\displaystyle x-1}" loading="lazy"></span> equals <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle (2^{N}-1)-(x-1)=}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mo stretchy="false">(</mo>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
</mrow>
</msup>
<mo>−<!-- − --></mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
<mo>−<!-- − --></mo>
<mo stretchy="false">(</mo>
<mi>x</mi>
<mo>−<!-- − --></mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
<mo>=</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle (2^{N}-1)-(x-1)=}</annotation>
</semantics>
</math></span><img src="./bb0b96db3c58077acce2fa3d6c0ee1b9751f4481.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:21.102ex; height:3.176ex;" alt="{\displaystyle (2^{N}-1)-(x-1)=}" loading="lazy"></span><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle (2^{N}-1)-x+1=}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mo stretchy="false">(</mo>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
</mrow>
</msup>
<mo>−<!-- − --></mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
<mo>−<!-- − --></mo>
<mi>x</mi>
<mo>+</mo>
<mn>1</mn>
<mo>=</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle (2^{N}-1)-x+1=}</annotation>
</semantics>
</math></span><img src="./5a9ccb12be7b325919078b9b63ac0a99d9df4afc.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:19.292ex; height:3.176ex;" alt="{\displaystyle (2^{N}-1)-x+1=}" loading="lazy"></span><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 2^{N}-x}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
</mrow>
</msup>
<mo>−<!-- − --></mo>
<mi>x</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle 2^{N}-x}</annotation>
</semantics>
</math></span><img src="./5dbf56f2535fd073f083e7ea417fc5f448dcd415.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.505ex; width:7.024ex; height:2.843ex;" alt="{\displaystyle 2^{N}-x}" loading="lazy"></span>, identical to the previous equation. Essentially, the subtraction inherent in the inversion operation changes the −1 added to <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle x}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>x</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle x}</annotation>
</semantics>
</math></span><img src="./87f9e315fd7e2ba406057a97300593c4802b53e4.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.33ex; height:1.676ex;" alt="{\displaystyle x}" loading="lazy"></span> before the inversion into +1 added after the inversion. This alternate subtract-and-invert algorithm to form a two's complement can sometimes be advantageous in computer programming or hardware design, for example where the subtraction of 1 can be obtained for free by incorporating it into an earlier operation.<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup>
</p><p>The two's complement of a negative number is the corresponding positive value, except in the special case of the <a href="Most_negative_number" class="mw-redirect" title="Most negative number">most negative number</a>. For example, inverting the bits of −5 (above) gives:
</p>
<div class="block-indent">0000 0100<sub>2</sub></div>
<p>And adding one gives the final value:
</p>
<div class="block-indent">0000 0101<sub>2</sub></div>
<p>The two's complement of the most negative number representable (e.g. a one as the most-significant bit and all other bits zero) is itself. Hence, there is an 'extra' negative number for which two's complement does not give the negation, see <a href="#Most_negative_number">§&nbsp;Most negative number</a> below.
</p><p>The case for the most negative number is one of only two special cases. The other special case is for zero, the two's complement of which is zero: inverting gives all ones, and adding one changes the ones back to zeros (since the overflow is ignored). Mathematically, in the two's complement system of signed integers (which represents the negative of each number as its two's complement), this is obviously correct: the negative of 0 is in fact 0 (<span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle -0=0}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mo>−<!-- − --></mo>
<mn>0</mn>
<mo>=</mo>
<mn>0</mn>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle -0=0}</annotation>
</semantics>
</math></span><img src="./018878a1f59f6c2e439e103e5f39c31ec3243ab1.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.505ex; width:7.232ex; height:2.343ex;" alt="{\displaystyle -0=0}" loading="lazy"></span>). This zero case also makes sense by the definition of two's complements: by that definition, the two's complement of zero would be <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 2^{N}-0=2^{N}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
</mrow>
</msup>
<mo>−<!-- − --></mo>
<mn>0</mn>
<mo>=</mo>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
</mrow>
</msup>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle 2^{N}-0=2^{N}}</annotation>
</semantics>
</math></span><img src="./10db3a113f062165b269fc8244c30229f31e7423.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.505ex; width:12.809ex; height:2.843ex;" alt="{\displaystyle 2^{N}-0=2^{N}}" loading="lazy"></span>, but in <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle N}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>N</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle N}</annotation>
</semantics>
</math></span><img src="./f5e3890c981ae85503089652feb48b191b57aae3.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:2.064ex; height:2.176ex;" alt="{\displaystyle N}" loading="lazy"></span> bits, all values are taken modulo <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 2^{N}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
</mrow>
</msup>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle 2^{N}}</annotation>
</semantics>
</math></span><img src="./f3cc22b5fa0e34487c8a6153965408e004c6e253.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:2.854ex; height:2.676ex;" alt="{\displaystyle 2^{N}}" loading="lazy"></span>, and <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 2^{N}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
</mrow>
</msup>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle 2^{N}}</annotation>
</semantics>
</math></span><img src="./f3cc22b5fa0e34487c8a6153965408e004c6e253.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:2.854ex; height:2.676ex;" alt="{\displaystyle 2^{N}}" loading="lazy"></span><span class="texhtml">mod </span><span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 2^{N}=0}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
</mrow>
</msup>
<mo>=</mo>
<mn>0</mn>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle 2^{N}=0}</annotation>
</semantics>
</math></span><img src="./c4f4e5763cf3b5f4f04b7936bd20175afca90329.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:7.115ex; height:2.676ex;" alt="{\displaystyle 2^{N}=0}" loading="lazy"></span>. In other words, the two's complement of 0 in <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle N}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>N</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle N}</annotation>
</semantics>
</math></span><img src="./f5e3890c981ae85503089652feb48b191b57aae3.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:2.064ex; height:2.176ex;" alt="{\displaystyle N}" loading="lazy"></span> bits is (by definition) a single 1 bit followed by <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle N}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>N</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle N}</annotation>
</semantics>
</math></span><img src="./f5e3890c981ae85503089652feb48b191b57aae3.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:2.064ex; height:2.176ex;" alt="{\displaystyle N}" loading="lazy"></span> zeros, but the 1 gets truncated, leaving 0.<sup id="cite_ref-7" class="reference"><a href="#cite_note-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup>
</p><p>In summary, the two's complement of any number, either positive, negative, or zero, can be computed in the same ways. In two's complement signed integer representation, the two's complement of any integer is equal to -1 times that integer, except for the most negative integer representable in the given number of bits <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle N}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>N</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle N}</annotation>
</semantics>
</math></span><img src="./f5e3890c981ae85503089652feb48b191b57aae3.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:2.064ex; height:2.176ex;" alt="{\displaystyle N}" loading="lazy"></span>, i.e. the integer <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle -2^{N-1}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mo>−<!-- − --></mo>
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
<mo>−<!-- − --></mo>
<mn>1</mn>
</mrow>
</msup>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle -2^{N-1}}</annotation>
</semantics>
</math></span><img src="./5f43fce86655c847fdbb85029446d60aee8edaee.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.505ex; width:6.763ex; height:2.843ex;" alt="{\displaystyle -2^{N-1}}" loading="lazy"></span>, the two's complement of which is itself (still negative).
</p>
<div class="mw-heading mw-heading3"><h3 id="Subtraction_from_2N">Subtraction from 2<sup><i>N</i></sup></h3></div>
<p>The sum of a number and its ones' complement is an <span class="texhtml mvar" style="font-style:italic;">N</span>-bit word with all 1 bits, which is (reading as an unsigned binary number) <span class="texhtml">2<sup><i>N</i></sup> − 1</span>. Then adding a number to its two's complement results in the <span class="texhtml mvar" style="font-style:italic;">N</span> lowest bits set to 0 and the carry bit 1, where the latter has the weight (reading it as an unsigned binary number) of <span class="texhtml">2<sup><i>N</i></sup></span>. Hence, in the unsigned binary arithmetic the value of two's-complement negative number <span class="texhtml"><i>x</i>*</span> of a positive <span class="texhtml mvar" style="font-style:italic;">x</span> satisfies the equality <span class="texhtml"><i>x</i>* = 2<sup><i>N</i></sup> − <i>x</i></span>.<sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span class="cite-bracket">[</span>a<span class="cite-bracket">]</span></a></sup>
</p><p>For example, to find the four-bit representation of −5 (subscripts denote the <a href="Radix" title="Radix">base of the representation</a>):
</p>
<div class="block-indent"><span class="texhtml"><i>x</i> = 5<sub>10</sub></span> therefore <span class="texhtml texhtml-big" style="font-size:120%;"><i>x</i> = 0101<sub>2</sub></span></div>
<p>Hence, with <span class="texhtml"><i>N</i> = 4</span>:
</p>
<div class="block-indent"><span class="texhtml"><i>x</i>* = 2<sup><i>N</i></sup> − <i>x</i> = 2<sup>4</sup> − 5<sub>10</sub> = 16<sub>10</sub> − 5<sub>10</sub> = 10000<sub>2</sub> − 0101<sub>2</sub> = 1011<sub>2</sub></span></div>
<p>The calculation can be done entirely in base 10, converting to base 2 at the end:
</p>
<div class="block-indent"><span class="texhtml"><i>x</i>* = 2<sup><i>N</i></sup> − <i>x</i> = 2<sup>4</sup> − 5<sub>10</sub> = 11<sub>10</sub> = 1011<sub>2</sub></span></div>
<div class="mw-heading mw-heading3"><h3 id="Working_from_LSB_towards_MSB">Working from LSB towards MSB</h3></div>
<p>A shortcut to manually convert a <a href="Binary_number" title="Binary number">binary number</a> into its two's complement is to start at the <a href="Least_significant_bit" class="mw-redirect" title="Least significant bit">least significant bit</a> (LSB), and copy all the zeros, working from LSB toward the most significant bit (MSB) until the first&nbsp;1 is reached; then copy that&nbsp;1, and flip all the remaining bits (Leave the MSB as a 1 if the initial number was in sign-and-magnitude representation). This shortcut allows a person to convert a number to its two's complement without first forming its ones' complement. For example: in two's complement representation, the negation of "0011&nbsp;1100" is "1100&nbsp;0<u>100</u>", where the underlined digits were unchanged by the copying operation (while the rest of the digits were flipped).
</p><p>In computer circuitry, this method is no faster than the "complement and add one" method; both methods require working sequentially from right to left, propagating logic changes. The method of complementing and adding one can be sped up by a standard <a href="Carry_look-ahead_adder" class="mw-redirect" title="Carry look-ahead adder">carry look-ahead adder</a> circuit; the LSB towards MSB method can be sped up by a similar logic transformation.
</p>
<div class="mw-heading mw-heading2"><h2 id="Sign_extension">Sign extension</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */


.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}


/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Sign_extension" title="Sign extension">Sign extension</a></div>
<table class="wikitable floatright" style="margin-left: 1.5em;text-align:center">
<caption>Sign-bit repetition in 7- and 8-bit integers using two's complement
</caption>
<tbody><tr>
<th>Decimal
</th>
<th>7-bit notation
</th>
<th>8-bit notation
</th></tr>
<tr>
<td style="text-align:right">−42 </td>
<td>1010110</td>
<td>1101 0110
</td></tr>
<tr>
<td style="text-align:right">42 </td>
<td>0101010</td>
<td>0010 1010
</td></tr></tbody></table>
<p>When turning a two's-complement number with a certain number of bits into one with more bits (e.g., when copying from a one-byte variable to a two-byte variable), the most-significant bit must be repeated in all the extra bits. Some processors do this in a single instruction; on other processors, a conditional must be used followed by code to set the relevant bits or bytes.
</p><p>Similarly, when a number is shifted to the right, the most-significant bit, which contains the sign information, must be maintained. However, when shifted to the left, a bit is shifted out. These rules preserve the common semantics that left shifts multiply the number by two and right shifts divide the number by two. However, if the most-significant bit changes from 0 to 1 (and vice versa), overflow is said to occur in the case that the value represents a signed integer.
</p><p>Both shifting and doubling the precision are important for some multiplication algorithms. Note that unlike addition and subtraction, width extension and right shifting are done differently for signed and unsigned numbers.
</p>
<div class="mw-heading mw-heading2"><h2 id="Most_negative_number">Most negative number</h2></div>
<p>With only one exception, starting with any number in two's-complement representation, if all the bits are flipped and 1 added, the two's-complement representation of the negative of that number is obtained. Positive&nbsp;12 becomes negative&nbsp;12, positive&nbsp;5 becomes negative&nbsp;5, zero becomes zero(+overflow), etc.
</p>
<table class="wikitable floatright" style="width:18em;text-align:center">
<caption><span class="anchor" id="−128_example_anchor">The two's complement of <span class="texhtml"> −128 </span></span>
</caption>
<tbody><tr>
<td><span class="texhtml"> −128 </span></td>
<td>1000 0000
</td></tr>
<tr>
<td>invert bits</td>
<td>0111 1111
</td></tr>
<tr>
<td>add one</td>
<td>1000 0000
</td></tr>
<tr>
<td colspan="2;"><span style="font-size: 85%;">Result is the same 8&nbsp;bit binary number.</span>
</td></tr></tbody></table>
<p>Taking the two's complement (negation) of the minimum number in the range will not have the desired effect of negating the number. For example, the two's complement of <span class="texhtml"> −128 </span> in an eight-bit system is <span class="texhtml"> −128&nbsp;,</span> as shown in the <a href="#−128_example_anchor">table to the right</a>. Although the expected result from negating <span class="texhtml"> −128 </span> is <span class="texhtml"> +128&nbsp;,</span> there is no representation of <span class="texhtml"> +128 </span> with an eight&nbsp;bit two's&nbsp;complement system and thus it is in fact impossible to represent the negation. Note that the two's complement being the same number is detected as an overflow condition since there was a carry into but not out of the most-significant bit.
</p><p>Having a nonzero number equal to its own negation is forced by the fact that zero is its own negation, and that the total number of numbers is even. Proof: there are <span class="texhtml"> 2^n − 1</span> nonzero numbers (an odd number). Negation would partition the nonzero numbers into sets of size 2, but this would result in the set of nonzero numbers having even cardinality. So at least one of the sets has size 1, i.e., a nonzero number is its own negation.
</p><p>The presence of the most negative number can lead to unexpected programming bugs where the result has an unexpected sign, or leads to an unexpected overflow exception, or leads to completely strange behaviors. For example,
</p>
<ul><li>the unary negation operator may not change the sign of a nonzero number. e.g., <span class="texhtml"> −(−128) &nbsp;⟼&nbsp; −128 &nbsp;</span> (where "<span class="texhtml">⟼</span>" is read as "becomes").</li>
<li>an implementation of <a href="Absolute_value" title="Absolute value">absolute value</a> may return a negative number;<sup id="cite_ref-9" class="reference"><a href="#cite_note-9"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup> e.g., <span class="texhtml">&nbsp; abs(−128) &nbsp;⟼&nbsp; −128&nbsp;.</span></li></ul>
<ul><li>Likewise, multiplication by <span class="texhtml"> −1 </span> may fail to function as expected; e.g., &nbsp; <span class="texhtml"> (−128) × (−1) &nbsp;⟼&nbsp; −128&nbsp;.</span></li>
<li>Division by <span class="texhtml"> −1 </span> may cause an exception (like that caused by dividing by <span class="texhtml"> 0 </span>);<sup id="cite_ref-10" class="reference"><a href="#cite_note-10"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup> even calculating the remainder (or <a href="Modulo" title="Modulo">modulo</a>) by <span class="texhtml"> −1 </span> can trigger this exception;<sup id="cite_ref-int32-c_11-0" class="reference"><a href="#cite_note-int32-c-11"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup> e.g., <span class="texhtml"> (−128) ÷ (−1) &nbsp;⟼&nbsp; <span style="font-size: 85%;">[<span class="smallcaps"><span style="font-variant: small-caps; text-transform: lowercase;">CRASH</span></span>]</span>&nbsp;,</span> &nbsp; <span class="texhtml"> (−128)&nbsp;% (−1) &nbsp;⟼&nbsp; <span style="font-size: 85%;">[<span class="smallcaps"><span style="font-variant: small-caps; text-transform: lowercase;">CRASH</span></span>]</span>&nbsp;.</span></li></ul>
<p>In the <a href="C_(programming_language)" title="C (programming language)">C</a> and <a href="C%2B%2B" title="C++">C++</a> programming languages, the above behaviours are <a href="Undefined_behavior" title="Undefined behavior">undefined</a> and not only may they return strange results, but the compiler is free to assume that the programmer has ensured that undefined numerical operations never happen, and make inferences from that assumption.<sup id="cite_ref-int32-c_11-1" class="reference"><a href="#cite_note-int32-c-11"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup> This enables a number of optimizations, but also leads to a number of strange bugs in programs with these undefined calculations.
</p><p>This most negative number in two's&nbsp;complement is sometimes called "the weird number", because it is the only exception.<sup id="cite_ref-12" class="reference"><a href="#cite_note-12"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-13" class="reference"><a href="#cite_note-13"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup>
Although the number is an exception, it is a valid number in regular two's&nbsp;complement systems. All arithmetic operations work with it both as an operand and (unless there was an overflow) a result.
</p>
<div class="mw-heading mw-heading2"><h2 id="Why_it_works">Why it works</h2></div>
<p>Given a set of all possible <span class="texhtml mvar" style="font-style:italic;">N</span>-bit values, we can assign the lower (by the binary value) half to be the integers from 0 to <span class="texhtml">(2<sup><i>N</i> − 1</sup> − 1)</span> inclusive and the upper half to be <span class="texhtml">−2<sup><i>N</i> − 1</sup></span> to −1 inclusive. The upper half (again, by the binary value) can be used to represent negative integers from <span class="texhtml">−2<sup><i>N</i> − 1</sup></span> to −1 because, under addition modulo <span class="texhtml">2<sup><i>N</i></sup></span> they behave the same way as those negative integers. That is to say that, because <span class="texhtml"><i>i</i> + <i>j</i> mod 2<sup><i>N</i></sup> = <i>i</i> + (<i>j</i> + 2<sup><i>N</i></sup>) mod 2<sup><i>N</i></sup></span>, any value in the set <span class="texhtml">{ <i>j</i> + <i>k</i> 2<sup><i>N</i></sup> | <i>k</i> is an integer } </span> can be used in place of&nbsp;<span class="texhtml mvar" style="font-style:italic;">j</span>.<sup id="cite_ref-14" class="reference"><a href="#cite_note-14"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup>
</p><p>For example, with eight bits, the unsigned bytes are 0 to 255. Subtracting 256 from the top half (128 to 255) yields the signed bytes −128 to −1.
</p><p>The relationship to two's complement is realised by noting that <span class="texhtml">256 = 255 + 1</span>, and <span class="texhtml">(255 − <i>x</i>)</span> is the <a href="Ones'_complement" title="Ones' complement">ones' complement</a> of&nbsp;<span class="texhtml mvar" style="font-style:italic;">x</span>.
</p>
<table class="wikitable floatright" style="width:14em;">
<caption>Some special numbers to note
</caption>
<tbody><tr>
<th>Decimal
</th>
<th>Binary (8-bit)
</th></tr>
<tr>
<td style="text-align:right">127 </td>
<td>0111 1111
</td></tr>
<tr>
<td style="text-align:right">64 </td>
<td>0100 0000
</td></tr>
<tr>
<td style="text-align:right">1  </td>
<td>0000 0001
</td></tr>
<tr>
<td style="text-align:right">0  </td>
<td>0000 0000
</td></tr>
<tr>
<td style="text-align:right">−1 </td>
<td>1111 1111
</td></tr>
<tr>
<td style="text-align:right">−64 </td>
<td>1100 0000
</td></tr>
<tr>
<td style="text-align:right">−127 </td>
<td>1000 0001
</td></tr>
<tr>
<td style="text-align:right">−128 </td>
<td>1000 0000
</td></tr></tbody></table>
<div class="mw-heading mw-heading3"><h3 id="Example">Example</h3></div>

<p>For example, an 8&nbsp;bit number can only represent every integer from −128. to 127., inclusive, since <span class="texhtml">(2<sup>8 − 1</sup> = 128.)</span>. <span class="nowrap">−95. modulo 256.</span> is equivalent to 161. since
</p>
<div class="block-indent">−95. + 256.</div>
<div class="block-indent">= −95. + 255. + 1</div>
<div class="block-indent">= 255. − 95. + 1</div>
<div class="block-indent">= 160. + 1.</div>
<div class="block-indent">= 161.</div>
<div style="overflow-x: auto;">
<pre style="width:25em"> 1111 1111 255.
− 0101 1111 − 95.
=========== =====
1010 0000 (ones' complement) 160.
+ 1 + 1
=========== =====
1010 0001 (two's complement) 161.
</pre>
</div>
<table class="wikitable floatright" style="width:14em;text-align:center">
<caption>Two's complement 4&nbsp;bit integer values
</caption>
<tbody><tr>
<th>Two's complement
</th>
<th>Decimal
</th></tr>
<tr>
<td>0111</td>
<td style="text-align:right">7. 
</td></tr>
<tr>
<td>0110</td>
<td style="text-align:right">6. 
</td></tr>
<tr>
<td>0101</td>
<td style="text-align:right">5. 
</td></tr>
<tr>
<td>0100</td>
<td style="text-align:right">4. 
</td></tr>
<tr>
<td>0011</td>
<td style="text-align:right">3. 
</td></tr>
<tr>
<td>0010</td>
<td style="text-align:right">2. 
</td></tr>
<tr>
<td>0001</td>
<td style="text-align:right">1. 
</td></tr>
<tr>
<td>0000</td>
<td style="text-align:right">0. 
</td></tr>
<tr>
<td>1111</td>
<td style="text-align:right">−1. 
</td></tr>
<tr>
<td>1110</td>
<td style="text-align:right">−2. 
</td></tr>
<tr>
<td>1101</td>
<td style="text-align:right">−3. 
</td></tr>
<tr>
<td>1100</td>
<td style="text-align:right">−4. 
</td></tr>
<tr>
<td>1011</td>
<td style="text-align:right">−5. 
</td></tr>
<tr>
<td>1010</td>
<td style="text-align:right">−6. 
</td></tr>
<tr>
<td>1001</td>
<td style="text-align:right">−7. 
</td></tr>
<tr>
<td>1000</td>
<td style="text-align:right">−8. 
</td></tr></tbody></table>
<p>Fundamentally, the system represents negative integers by counting backward and <a href="Modular_arithmetic" title="Modular arithmetic">wrapping around</a>. The boundary between positive and negative numbers is arbitrary, but by <a href="Convention_(norm)" title="Convention (norm)">convention</a> all negative numbers have a left-most bit (<a href="Most_significant_bit" class="mw-redirect" title="Most significant bit">most significant bit</a>) of one. Therefore, the most positive four-bit number is 0111&nbsp;(7.) and the most negative is 1000 (−8.). Because of the use of the left-most bit as the sign bit, the absolute value of the most negative number (|−8.| = 8.) is too large to represent. Negating a two's complement number is simple: Invert all the bits and add one to the result. For example, negating 1111, we get <span class="nowrap">0000 + 1 = 1</span>. Therefore, 1111 in binary must represent −1 in decimal.<sup id="cite_ref-15" class="reference"><a href="#cite_note-15"><span class="cite-bracket">[</span>14<span class="cite-bracket">]</span></a></sup>
</p><p>The system is useful in simplifying the implementation of arithmetic on computer hardware. Adding 0011&nbsp;(3.) to 1111&nbsp;(−1.) at first seems to give the incorrect answer of 10010. However, the hardware can simply ignore the left-most bit to give the correct answer of 0010&nbsp;(2.). Overflow checks still must exist to catch operations such as summing 0100 and 0100.
</p><p>The system therefore allows addition of negative operands without a subtraction circuit or a circuit that detects the sign of a number. Moreover, that addition circuit can also perform subtraction by taking the two's complement of a number (see below), which only requires an additional cycle or its own adder circuit. To perform this, the circuit merely operates as if there were an extra left-most bit of 1.
</p>
<div class="mw-heading mw-heading2"><h2 id="Arithmetic_operations">Arithmetic operations</h2></div>
<div class="mw-heading mw-heading3"><h3 id="Addition">Addition</h3></div>
<p>Adding two's complement numbers requires no special processing even if the operands have opposite signs; the sign of the result is determined automatically. For example, adding 15 and −5:
</p>
<div style="overflow-x: auto;">
<pre style="width:25em;"> 0000 1111 (15)
+ 1111 1011 (−5)
===========
0000 1010 (10)
</pre>
</div>
<p>Or the computation of 5 − 15 = 5 + (−15):
</p>
<div style="overflow-x: auto;">
<pre style="width:25em"> 0000 0101 ( 5)
+ 1111 0001 (−15)
===========
1111 0110 (−10)
</pre>
</div>
<p>This process depends upon restricting to 8 bits of precision; a carry to the (nonexistent) 9th most significant bit is ignored, resulting in the arithmetically correct result of 10<sub>10</sub>.
</p><p>The last two bits of the <a href="Carry_flag" title="Carry flag">carry</a> row (reading right-to-left) contain vital information: whether the calculation resulted in an <a href="Arithmetic_overflow" class="mw-redirect" title="Arithmetic overflow">arithmetic overflow</a>, a number too large for the binary system to represent (in this case greater than 8 bits). An overflow condition exists when these last two bits are different from one another. As mentioned above, the sign of the number is encoded in the MSB of the result.
</p><p>In other terms, if the left two carry bits (the ones on the far left of the top row in these examples) are both 1s or both 0s, the result is valid; if the left two carry bits are "1 0" or "0 1", a sign overflow has occurred. Conveniently, an <a href="XOR" class="mw-redirect" title="XOR">XOR</a> operation on these two bits can quickly determine if an overflow condition exists. As an example, consider the signed 4-bit addition of 7 and 3:
</p>
<div style="overflow-x: auto;">
<pre style="width:25em"> 0111 (carry)
0111 (7)
+ 0011 (3)
======
1010 (−6) invalid!
</pre>
</div>
<p>In this case, the far left two (MSB) carry bits are "01", which means there was a two's-complement addition overflow. That is, 1010<sub>2</sub> = 10<sub>10</sub> is outside the permitted range of −8 to 7. The result would be correct if treated as unsigned integer.
</p><p>In general, any two <span class="texhtml mvar" style="font-style:italic;">N</span>-bit numbers may be added <i>without</i> overflow, by first sign-extending both of them to <span class="texhtml"><i>N</i> + 1</span> bits, and then adding as above. The <span class="texhtml"><i>N</i> + 1</span> bits result is large enough to represent any possible sum (<span class="texhtml"><i>N</i> = 5</span> two's complement can represent values in the range −16 to 15) so overflow will never occur. It is then possible, if desired, to 'truncate' the result back to <span class="texhtml mvar" style="font-style:italic;">N</span> bits while preserving the value if and only if the discarded bit is a proper sign extension of the retained result bits. This provides another method of detecting overflow – which is equivalent to the method of comparing the carry bits – but which may be easier to implement in some situations, because it does not require access to the internals of the addition.
</p>
<div class="mw-heading mw-heading3"><h3 id="Subtraction">Subtraction</h3></div>
<p>Computers usually use the <a href="Method_of_complements" title="Method of complements">method of complements</a> to implement subtraction. Using complements for subtraction is closely related to using complements for representing negative numbers, since the combination allows all signs of operands and results; direct subtraction works with two's-complement numbers as well. Like addition, the advantage of using two's complement is the elimination of examining the signs of the operands to determine whether addition or subtraction is needed. For example, subtracting −5 from 15 is really adding 5 to 15, but this is hidden by the two's-complement representation:
</p>
<div style="overflow-x: auto;">
<pre style="width:25em"> 11110 000 (borrow)
0000 1111 (15)
− 1111 1011 (−5)
===========
0001 0100 (20)
</pre>
</div>
<p>Overflow is detected the same way as for addition, by examining the two leftmost (most significant) bits of the borrows; overflow has occurred if they are different.
</p><p>Another example is a subtraction operation where the result is negative: 15&nbsp;−&nbsp;35 = −20:
</p>
<div style="overflow-x: auto;">
<pre style="width:25em"> 11100 000 (borrow)
0000 1111 (15)
− 0010 0011 (35)
===========
1110 1100 (−20)
</pre>
</div>
<p>As for addition, overflow in subtraction may be avoided (or detected after the operation) by first sign-extending both inputs by an extra bit.
</p>
<div class="mw-heading mw-heading3"><h3 id="Multiplication">Multiplication</h3></div>
<p>The product of two <span class="texhtml mvar" style="font-style:italic;">N</span>-bit numbers requires <span class="texhtml">2<i>N</i></span> bits to contain all possible values.<sup id="cite_ref-16" class="reference"><a href="#cite_note-16"><span class="cite-bracket">[</span>15<span class="cite-bracket">]</span></a></sup>
</p><p>If the precision of the two operands using two's complement is doubled before the multiplication, direct multiplication (discarding any excess bits beyond that precision) will provide the correct result.<sup id="cite_ref-17" class="reference"><a href="#cite_note-17"><span class="cite-bracket">[</span>16<span class="cite-bracket">]</span></a></sup> For example, take <span class="texhtml">6 × (−5) = −30</span>. First, the precision is extended from four bits to eight. Then the numbers are multiplied, discarding the bits beyond the eighth bit (as shown by "<style data-mw-deduplicate="TemplateStyles:r886049734">
/* start https://en.wikipedia.org/ */


.mw-parser-output .monospaced{font-family:monospace,monospace}


/* end https://en.wikipedia.org/ */
</style><span class="monospaced">x</span>"):
</p>
<div style="overflow-x: auto;">
<pre style="width:25em"> 00000110 (6)
* 11111011 (−5)
============
110
1100
00000
110000
1100000
11000000
x10000000
+ xx00000000
============
xx11100010
</pre>
</div>
<p>This is very inefficient; by doubling the precision ahead of time, all additions must be double-precision and at least twice as many partial products are needed than for the more efficient algorithms actually implemented in computers. Some multiplication algorithms are designed for two's complement, notably <a href="Booth's_multiplication_algorithm" title="Booth's multiplication algorithm">Booth's multiplication algorithm</a>. Methods for multiplying sign-magnitude numbers do not work with two's-complement numbers without adaptation. There is not usually a problem when the multiplicand (the one being repeatedly added to form the product) is negative; the issue is setting the initial bits of the product correctly when the multiplier is negative. Two methods for adapting algorithms to handle two's-complement numbers are common:
</p>
<ul><li>First check to see if the multiplier is negative. If so, negate (i.e., take the two's complement of) both operands before multiplying. The multiplier will then be positive so the algorithm will work. Because both operands are negated, the result will still have the correct sign.</li>
<li>Subtract the partial product resulting from the MSB (pseudo sign bit) instead of adding it like the other partial products. This method requires the multiplicand's sign bit to be extended by one position, being preserved during the shift right actions.<sup id="cite_ref-18" class="reference"><a href="#cite_note-18"><span class="cite-bracket">[</span>17<span class="cite-bracket">]</span></a></sup></li></ul>
<p>As an example of the second method, take the common add-and-shift algorithm for multiplication. Instead of shifting partial products to the left as is done with pencil and paper, the accumulated product is shifted right, into a second register that will eventually hold the least significant half of the product. Since the <a href="Least_significant_bit" class="mw-redirect" title="Least significant bit">least significant bits</a> are not changed once they are calculated, the additions can be single precision, accumulating in the register that will eventually hold the most significant half of the product. In the following example, again multiplying 6 by −5, the two registers and the extended sign bit are separated by "|":
</p>
<pre style="width:90%;overflow:scroll;white-space:pre"> 0 0110 (6) (multiplicand with extended sign bit)
× 1011 (−5) (multiplier)
=|====|====
0|0110|0000 (first partial product (rightmost bit is 1))
0|0011|0000 (shift right, preserving extended sign bit)
0|1001|0000 (add second partial product (next bit is 1))
0|0100|1000 (shift right, preserving extended sign bit)
0|0100|1000 (add third partial product: 0 so no change)
0|0010|0100 (shift right, preserving extended sign bit)
1|1100|0100 (subtract last partial product since it's from sign bit)
1|1110|0010 (shift right, preserving extended sign bit)
|1110|0010 (discard extended sign bit, giving the final answer, −30)
</pre>
<div class="mw-heading mw-heading3"><h3 id="Comparison_(ordering)">Comparison (ordering)</h3></div>
<p><a href="Comparison_(computer_programming)" class="mw-redirect" title="Comparison (computer programming)">Comparison</a> is often implemented with a dummy subtraction, where the flags in the computer's <a href="Status_register" title="Status register">status register</a> are checked, but the main result is ignored. The <a href="Zero_flag" title="Zero flag">zero flag</a> indicates if two values compared equal. If the exclusive-or of the <a href="Sign_flag" class="mw-redirect" title="Sign flag">sign</a> and <a href="Overflow_flag" title="Overflow flag">overflow</a> flags is 1, the subtraction result was less than zero, otherwise the result was zero or greater. These checks are often implemented in computers in <a href="Conditional_branch" class="mw-redirect" title="Conditional branch">conditional branch</a> instructions.
</p><p>Unsigned binary numbers can be ordered by a simple <a href="Lexicographic_ordering" class="mw-redirect" title="Lexicographic ordering">lexicographic ordering</a>, where the bit value 0 is defined as less than the bit value 1. For two's complement values, the meaning of the most significant bit is reversed (i.e. 1 is less than 0).
</p><p>The following algorithm (for an <span class="texhtml mvar" style="font-style:italic;">n</span>-bit two's complement architecture) sets the result register R to −1 if A &lt; B, to +1 if A &gt; B, and to 0 if A and B are equal:
</p>
<div class="mw-highlight mw-highlight-lang-pascal mw-content-ltr" dir="ltr"><pre><span class="c1">// reversed comparison of the sign bit</span>

<span class="k">if</span><span class="w"> </span><span class="n">A</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="k">and</span><span class="w"> </span><span class="n">B</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="k">then</span>
<span class="w"> </span><span class="n">return</span><span class="w"> </span><span class="o">+</span><span class="mi">1</span><span class="o">;</span>
<span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">A</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="k">and</span><span class="w"> </span><span class="n">B</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="k">then</span>
<span class="w"> </span><span class="n">return</span><span class="w"> </span><span class="o">-</span><span class="mi">1</span>
<span class="k">end</span><span class="o">;</span>
<span class="w"> </span>
<span class="c1">// comparison of remaining bits</span>

<span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">n</span><span class="o">-</span><span class="mi">2</span><span class="w"> </span><span class="k">downto</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="k">do</span><span class="w"> </span><span class="k">begin</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">A</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="k">and</span><span class="w"> </span><span class="n">B</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="k">then</span>
<span class="w"> </span><span class="n">return</span><span class="w"> </span><span class="o">-</span><span class="mi">1</span>
<span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">A</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="k">and</span><span class="w"> </span><span class="n">B</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="k">then</span>
<span class="w"> </span><span class="n">return</span><span class="w"> </span><span class="o">+</span><span class="mi">1</span><span class="w"> </span>
<span class="w"> </span><span class="k">end</span>
<span class="k">end</span>
<span class="w"> </span>
<span class="n">return</span><span class="w"> </span><span class="mi">0</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="Two's_complement_and_2-adic_numbers">Two's complement and <span class="nowrap">2-adic</span> numbers</h2></div>
<p>In a classic <i><a href="HAKMEM" title="HAKMEM">HAKMEM</a></i> published by the <a href="MIT_AI_Lab" class="mw-redirect" title="MIT AI Lab">MIT AI Lab</a> in 1972, <a href="Bill_Gosper" title="Bill Gosper">Bill Gosper</a> noted that whether or not a machine's internal representation was two's-complement could be determined by summing the successive powers of two. In a flight of fancy, he noted that the result of doing this algebraically indicated that "algebra is run on a machine (the universe) which is two's-complement."<sup id="cite_ref-19" class="reference"><a href="#cite_note-19"><span class="cite-bracket">[</span>18<span class="cite-bracket">]</span></a></sup>
</p><p>Gosper's end conclusion is not necessarily meant to be taken seriously, and it is akin to a <a href="Mathematical_joke" title="Mathematical joke">mathematical joke</a>. The critical step is "...110 = ...111&nbsp;−&nbsp;1", i.e., "2<i>X</i> = <i>X</i>&nbsp;−&nbsp;1", and thus <i>X</i>&nbsp;=&nbsp;...111&nbsp;=&nbsp;−1. This presupposes a method by which an infinite string of 1s is considered a number, which requires an extension of the finite place-value concepts in elementary arithmetic. It is meaningful either as part of a two's-complement notation for all integers, as a typical <a href="P-adic_number" title="P-adic number">2-adic number</a>, or even as one of the generalized sums defined for the <a href="Divergent_series" title="Divergent series">divergent series</a> of real numbers <a href="1_%2B_2_%2B_4_%2B_8_%2B_%E2%8B%AF" title="1 + 2 + 4 + 8 + ⋯">1 + 2 + 4 + 8 + ⋯</a>.<sup id="cite_ref-20" class="reference"><a href="#cite_note-20"><span class="cite-bracket">[</span>19<span class="cite-bracket">]</span></a></sup> Digital arithmetic circuits, idealized to operate with infinite (extending to positive powers of 2) bit strings, produce 2-adic addition and multiplication compatible with two's complement representation.<sup id="cite_ref-21" class="reference"><a href="#cite_note-21"><span class="cite-bracket">[</span>20<span class="cite-bracket">]</span></a></sup> <a href="Continuous_function" title="Continuous function">Continuity</a> of binary arithmetical and <a href="Bitwise_operation" title="Bitwise operation">bitwise operations</a> in 2-adic <a href="Metric_space" title="Metric space">metric</a> also has some use in cryptography.<sup id="cite_ref-22" class="reference"><a href="#cite_note-22"><span class="cite-bracket">[</span>21<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="Fraction_conversion">Fraction conversion</h2></div>
<p>To convert a number with a fractional part, such as .0101, one must convert starting from right to left the 1s to decimal as in a normal conversion. In this example 0101 is equal to 5 in decimal. Each digit after the floating point represents a fraction where the denominator is a multiplier of 2. So, the first is 1/2, the second is 1/4 and so on. Having already calculated the decimal value as mentioned above, only the denominator of the LSB (LSB = starting from right) is used. The final result of this conversion is 5/16.
</p><p>For instance, having the floating value of .0110 for this method to work, one should not consider the last 0 from the right. Hence, instead of calculating the decimal value for 0110, we calculate the value 011, which is 3 in decimal (by leaving the 0 in the end, the result would have been 6, together with the denominator 2<sup>4</sup>&nbsp;=&nbsp;16, which reduces to 3/8). The denominator is 8, giving a final result of 3/8.
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Ones'_complement" title="Ones' complement">Ones' complement</a>, an alternative binary number convention</li>
<li><a href="Division_algorithm" title="Division algorithm">Division algorithm</a>, including restoring and non-restoring division in two's-complement representations</li>
<li><a href="Offset_binary" title="Offset binary">Offset binary</a></li>
<li><a href="P-adic_number" title="P-adic number"><i>p</i>-adic number</a></li>
<li><a href="Method_of_complements" title="Method of complements">Method of complements</a>, generalisation to other number bases, used on mechanical calculators</li></ul>
<div class="mw-heading mw-heading2"><h2 id="Notes">Notes</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */


.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}


/* end https://en.wikipedia.org/ */
</style><div class="reflist reflist-lower-alpha">
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-8">^</a></b></span> <span class="reference-text">For <span class="texhtml"><i>x</i> = 0</span> we have <span class="texhtml">2<sup><i>N</i></sup> − 0 = 2<sup><i>N</i></sup></span>, which is equivalent to <span class="texhtml">0* = 0</span> modulo <span class="texhtml">2<sup><i>N</i></sup></span> (i.e. after restricting to <span class="texhtml mvar" style="font-style:italic;">N</span> least significant bits).</span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<div class="reflist reflist-columns references-column-width" style="column-width: 30em;">
<ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text">E.g. "Signed integers are two's complement binary values that can be used to represent both positive and negative integer values", Section 4.2.1 in <i>Intel 64 and IA-32 Architectures Software Developer's Manual</i>, Volume 1: Basic Architecture, November 2006</span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text">
<style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */


.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}


/* end https://en.wikipedia.org/ */
</style><cite id="CITEREFBergelCassouDucasseLaval2013" class="citation book cs1">Bergel, Alexandre; Cassou, Damien; Ducasse, Stéphane; Laval, Jannik (2013). <a rel="nofollow" class="external text" href="http://files.pharo.org/books-pdfs/deep-into-pharo/2013-DeepIntoPharo-EN.pdf"><i>Deep into Pharo</i></a> <span class="cs1-format">(PDF)</span>. p.&nbsp;337.</cite></span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://www.rit.edu/academicsuccesscenter/sites/rit.edu.academicsuccesscenter/files/documents/math-handouts/DM3_TwosComplement_BP_9_22_14.pdf">"Two's Complement"</a> <span class="cs1-format">(PDF)</span>. <i>University of Rochester Academic Success Center</i>.</cite></span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><cite id="CITEREFLiljaSapatnekar2005" class="citation book cs1">Lilja, David J.; Sapatnekar, Sachin S. (2005). <a rel="nofollow" class="external text" href="https://books.google.com/books?id=5BvW0hYhxkQC&amp;dq=%22two%27s+complement+arithmetic%22&amp;pg=PA37"><i>Designing Digital Computer Systems with Verilog</i></a>. Cambridge University Press. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a>&nbsp;<bdi>9780521828666</bdi>.</cite></span>
</li>
<li id="cite_note-von_Neumann_1945-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-von_Neumann_1945_5-0">^</a></b></span> <span class="reference-text"><cite id="CITEREFvon_Neumann1945" class="citation cs2"><a href="John_von_Neumann" title="John von Neumann">von Neumann, John</a> (1945), <a rel="nofollow" class="external text" href="http://web.mit.edu/STS.035/www/PDFs/edvac.pdf"><i>First Draft of a Report on the EDVAC</i></a> <span class="cs1-format">(PDF)</span><span class="reference-accessdate">, retrieved <span class="nowrap">February 20,</span> 2021</span></cite></span>
</li>
<li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text">... e.g. by reducing an added constant by 1, increasing a subtracted constant by 1, or setting the carry/borrow flag before a subtract-with-borrow operation. For example, to compute <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle -(m+4)}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mo>−<!-- − --></mo>
<mo stretchy="false">(</mo>
<mi>m</mi>
<mo>+</mo>
<mn>4</mn>
<mo stretchy="false">)</mo>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle -(m+4)}</annotation>
</semantics>
</math></span><img src="./3f59b2587dd4fdd460fb73cefe5d3db37099fdb6.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.838ex; width:9.661ex; height:2.843ex;" alt="{\displaystyle -(m+4)}" loading="lazy"></span>, instead of adding 4 to <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle m}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>m</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle m}</annotation>
</semantics>
</math></span><img src="./0a07d98bb302f3856cbabc47b2b9016692e3f7bc.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:2.04ex; height:1.676ex;" alt="{\displaystyle m}" loading="lazy"></span>, inverting the result, and then adding 1, one can merely add 3 (= 4 − 1) to <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle m}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>m</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle m}</annotation>
</semantics>
</math></span><img src="./0a07d98bb302f3856cbabc47b2b9016692e3f7bc.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:2.04ex; height:1.676ex;" alt="{\displaystyle m}" loading="lazy"></span> and then invert the result. (Of course, it is also an option, using the invert-and-add scheme, to invert <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle m}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>m</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle m}</annotation>
</semantics>
</math></span><img src="./0a07d98bb302f3856cbabc47b2b9016692e3f7bc.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:2.04ex; height:1.676ex;" alt="{\displaystyle m}" loading="lazy"></span> first and then subtract 3 [equivalent to adding −3 = −4 + 1].)</span>
</li>
<li id="cite_note-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-7">^</a></b></span> <span class="reference-text">Zero is the one value which when added to its two's complement, using machine (modular) binary arithmetic, does not sum to <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 2^{N}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
</mrow>
</msup>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle 2^{N}}</annotation>
</semantics>
</math></span><img src="./f3cc22b5fa0e34487c8a6153965408e004c6e253.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:2.854ex; height:2.676ex;" alt="{\displaystyle 2^{N}}" loading="lazy"></span>, but observe that in arithmetic modulo <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 2^{N}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
</mrow>
</msup>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle 2^{N}}</annotation>
</semantics>
</math></span><img src="./f3cc22b5fa0e34487c8a6153965408e004c6e253.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:2.854ex; height:2.676ex;" alt="{\displaystyle 2^{N}}" loading="lazy"></span>, <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 0}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mn>0</mn>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle 0}</annotation>
</semantics>
</math></span><img src="./2aae8864a3c1fec9585261791a809ddec1489950.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.162ex; height:2.176ex;" alt="{\displaystyle 0}" loading="lazy"></span> is congruent to <span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle 2^{N}}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<msup>
<mn>2</mn>
<mrow class="MJX-TeXAtom-ORD">
<mi>N</mi>
</mrow>
</msup>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle 2^{N}}</annotation>
</semantics>
</math></span><img src="./f3cc22b5fa0e34487c8a6153965408e004c6e253.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:2.854ex; height:2.676ex;" alt="{\displaystyle 2^{N}}" loading="lazy"></span>.</span>
</li>
<li id="cite_note-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-9">^</a></b></span> <span class="reference-text">
<cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html">"Math"</a>. API specification. Java Platform SE&nbsp;7.</cite></span>
</li>
<li id="cite_note-10"><span class="mw-cite-backlink"><b><a href="#cite_ref-10">^</a></b></span> <span class="reference-text">
<cite id="CITEREFRegehr2013" class="citation web cs1">Regehr, John (2013). <a rel="nofollow" class="external text" href="https://blog.regehr.org/archives/887">"Nobody expects the Spanish inquisition, or INT_MIN to be divided by −1"</a>. <i>Regehr.org</i> (blog).</cite></span>
</li>
<li id="cite_note-int32-c-11"><span class="mw-cite-backlink">^ <a href="#cite_ref-int32-c_11-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-int32-c_11-1"><sup><i><b>b</b></i></sup></a></span> <span class="reference-text">
<cite id="CITEREFSeacord2020" class="citation web cs1">Seacord, Robert C. (2020). <a rel="nofollow" class="external text" href="https://wiki.sei.cmu.edu/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow">"Ensure that operations on signed integers do not result in overflow"</a>. Rule INT32-C. <i>wiki.sei.cmu.edu</i>. SEI CERT C&nbsp;Coding Standard.</cite></span>
</li>
<li id="cite_note-12"><span class="mw-cite-backlink"><b><a href="#cite_ref-12">^</a></b></span> <span class="reference-text">
<cite id="CITEREFAffeldtMarti2006" class="citation report cs1">Affeldt, Reynald &amp; Marti, Nicolas (2006). <a rel="nofollow" class="external text" href="https://web.archive.org/web/20110722080531/http://www.ipl.t.u-tokyo.ac.jp/jssst2006/papers/Affeldt.pdf">Formal verification of arithmetic functions in SmartMIPS Assembly</a> <span class="cs1-format">(PDF)</span> (Report). Archived from <a rel="nofollow" class="external text" href="http://www.ipl.t.u-tokyo.ac.jp/jssst2006/papers/Affeldt.pdf">the original</a> <span class="cs1-format">(PDF)</span> on 2011-07-22.</cite></span>
</li>
<li id="cite_note-13"><span class="mw-cite-backlink"><b><a href="#cite_ref-13">^</a></b></span> <span class="reference-text">
<cite id="CITEREFHarrisHarris2007" class="citation book cs1">Harris, David Money; Harris, Sarah L. (2007). <a rel="nofollow" class="external text" href="https://books.google.com/books?id=5X7JV5-n0FIC&amp;q=%22weird+number%22+binary&amp;pg=PA19"><i>Digital Design and Computer Architecture</i></a>. Morgan Kaufmann. p.&nbsp;18. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a>&nbsp;<bdi>978-0-08-054706-0</bdi> – via Google Books.</cite></span>
</li>
<li id="cite_note-14"><span class="mw-cite-backlink"><b><a href="#cite_ref-14">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20131031093811/http://www.cs.uwm.edu/~cs151/Bacon/Lecture/HTML/ch03s09.html">"3.9. Two's Complement"</a>. <i>Chapter 3. Data Representation</i>. cs.uwm.edu. 2012-12-03. Archived from <a rel="nofollow" class="external text" href="http://www.cs.uwm.edu/~cs151/Bacon/Lecture/HTML/ch03s09.html">the original</a> on 31 October 2013<span class="reference-accessdate">. Retrieved <span class="nowrap">2014-06-22</span></span>.</cite></span>
</li>
<li id="cite_note-15"><span class="mw-cite-backlink"><b><a href="#cite_ref-15">^</a></b></span> <span class="reference-text"><cite id="CITEREFFinley2000" class="citation web cs1">Finley, Thomas (April 2000). <a rel="nofollow" class="external text" href="http://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html">"Two's Complement"</a>. Computer Science. Class notes for CS&nbsp;104. Ithaca, New York: Cornell University<span class="reference-accessdate">. Retrieved <span class="nowrap">2014-06-22</span></span>.</cite></span>
</li>
<li id="cite_note-16"><span class="mw-cite-backlink"><b><a href="#cite_ref-16">^</a></b></span> <span class="reference-text">Bruno Paillard. <i>An Introduction To Digital Signal Processors</i>, Sec. 6.4.2. Génie électrique et informatique Report, Université de Sherbrooke, April 2004.</span>
</li>
<li id="cite_note-17"><span class="mw-cite-backlink"><b><a href="#cite_ref-17">^</a></b></span> <span class="reference-text"><cite id="CITEREFKaren_Miller2007" class="citation web cs1">Karen Miller (August 24, 2007). <a rel="nofollow" class="external text" href="https://web.archive.org/web/20150213203512/http://pages.cs.wisc.edu/~cs354-1/beyond354/int.mult.html">"Two's Complement Multiplication"</a>. <i>cs.wisc.edu</i>. Archived from <a rel="nofollow" class="external text" href="http://pages.cs.wisc.edu/~cs354-1/beyond354/int.mult.html">the original</a> on February 13, 2015<span class="reference-accessdate">. Retrieved <span class="nowrap">April 13,</span> 2015</span>.</cite></span>
</li>
<li id="cite_note-18"><span class="mw-cite-backlink"><b><a href="#cite_ref-18">^</a></b></span> <span class="reference-text"><cite id="CITEREFWakerly2000" class="citation book cs1">Wakerly, John F. (2000). <i>Digital Design Principles &amp; Practices</i> (3rd&nbsp;ed.). Prentice Hall. p.&nbsp;47. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a>&nbsp;<bdi>0-13-769191-2</bdi>.</cite></span>
</li>
<li id="cite_note-19"><span class="mw-cite-backlink"><b><a href="#cite_ref-19">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20240224184437/http://www.inwap.com/pdp10/hbaker/hakmem/hacks.html#item154">"Programming Hacks"</a>. <i>HAKMEM</i>. ITEM 154 (Gosper). Archived from <a rel="nofollow" class="external text" href="http://www.inwap.com/pdp10/hbaker/hakmem/hacks.html#item154">the original</a> on 2024-02-24.</cite></span>
</li>
<li id="cite_note-20"><span class="mw-cite-backlink"><b><a href="#cite_ref-20">^</a></b></span> <span class="reference-text">For the summation of 1 + 2 + 4 + 8 + ⋯ without recourse to the 2-adic metric, see <cite id="CITEREFHardy1949" class="citation book cs1"><a href="G._H._Hardy" title="G. H. Hardy">Hardy, G. H.</a> (1949). <i>Divergent Series</i>. Clarendon Press. pp.&nbsp;<span class="nowrap">7–</span>10. <a href="LCC_(identifier)" class="mw-redirect" title="LCC (identifier)">LCC</a>&nbsp;<a rel="nofollow" class="external text" href="https://catalog.loc.gov/vwebv/search?searchCode=CALL%2B&amp;searchArg=QA295+.H29+1967&amp;searchType=1&amp;recCount=25">QA295&nbsp;.H29&nbsp;1967</a>.</cite></span>
</li>
<li id="cite_note-21"><span class="mw-cite-backlink"><b><a href="#cite_ref-21">^</a></b></span> <span class="reference-text"><cite id="CITEREFVuillemin1993" class="citation book cs1">Vuillemin, Jean (1993). "Chapter 7". <a rel="nofollow" class="external text" href="https://hplabs.itcs.hp.com/techreports/Compaq-DEC/PRL-RR-25.pdf"><i>On circuits and numbers</i></a> <span class="cs1-format">(PDF)</span>. Paris: <a href="Digital_Equipment_Corporation" title="Digital Equipment Corporation">Digital Equipment Corporation</a>. p.&nbsp;19<span class="reference-accessdate">. Retrieved <span class="nowrap">2023-03-29</span></span>.</cite> See especially chapter 7.3 for multiplication.</span>
</li>
<li id="cite_note-22"><span class="mw-cite-backlink"><b><a href="#cite_ref-22">^</a></b></span> <span class="reference-text"><cite id="CITEREFAnashinBogdanovKizhvatov2007" class="citation web cs1">Anashin, Vladimir; Bogdanov, Andrey; Kizhvatov, Ilya (2007). <a rel="nofollow" class="external text" href="https://crypto.rsuh.ru/">"ABC Stream Cipher"</a>. <a href="Russian_State_University_for_the_Humanities" title="Russian State University for the Humanities">Russian State University for the Humanities</a><span class="reference-accessdate">. Retrieved <span class="nowrap">24 January</span> 2012</span>.</cite></span>
</li>
</ol></div>
<div class="mw-heading mw-heading2"><h2 id="Further_reading">Further reading</h2></div>
<ul><li><a rel="nofollow" class="external text" href="https://www.cs.cornell.edu/~tomf/notes/cps104/twoscomp.html">Two's Complement Explanation</a> (Thomas Finley, 2000)</li>
<li><cite id="CITEREFKoren2002" class="citation book cs1">Koren, Israel (2002). <i>Computer Arithmetic Algorithms</i>. A. K. Peters. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a>&nbsp;<bdi>1-56881-160-8</bdi>.</cite></li>
<li><cite id="CITEREFFlores1963" class="citation book cs1">Flores, Ivan (1963). <i>The Logic of Computer Arithmetic</i>. Prentice-Hall.</cite></li></ul>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<ul><li><a rel="nofollow" class="external text" href="http://www.ecs.umass.edu/ece/koren/arith/simulator/ArrMlt/">Two's complement array multiplier JavaScript simulator</a></li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-07-28" href="https://en.wikipedia.org/wiki/?title=Two's_complement&amp;oldid=1302976813">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>

</body></html>